{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Signal_Similarity-blog.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "69gfCzIIphFg",
        "outputId": "8b28529e-2273-4763-cdd7-16092a2190a1"
      },
      "source": [
        "!pip install deep-xf"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Collecting deep-xf\n",
            "  Downloading deep_xf-0.0.5-py3-none-any.whl (35 kB)\n",
            "Requirement already satisfied: tqdm in /usr/local/lib/python3.7/dist-packages (from deep-xf) (4.62.3)\n",
            "Requirement already satisfied: jupyter in /usr/local/lib/python3.7/dist-packages (from deep-xf) (1.0.0)\n",
            "Collecting shap==0.39.0\n",
            "  Downloading shap-0.39.0.tar.gz (356 kB)\n",
            "\u001b[K     |████████████████████████████████| 356 kB 7.3 MB/s \n",
            "\u001b[?25hRequirement already satisfied: torch in /usr/local/lib/python3.7/dist-packages (from deep-xf) (1.9.0+cu111)\n",
            "Collecting statsmodels==0.12.2\n",
            "  Downloading statsmodels-0.12.2-cp37-cp37m-manylinux1_x86_64.whl (9.5 MB)\n",
            "\u001b[K     |████████████████████████████████| 9.5 MB 37.3 MB/s \n",
            "\u001b[?25hCollecting scikit-learn>=0.24.0\n",
            "  Downloading scikit_learn-1.0.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (23.2 MB)\n",
            "\u001b[K     |████████████████████████████████| 23.2 MB 1.6 MB/s \n",
            "\u001b[?25hCollecting py-ecg-detectors\n",
            "  Downloading py_ecg_detectors-1.1.0-py3-none-any.whl (23 kB)\n",
            "Requirement already satisfied: matplotlib in /usr/local/lib/python3.7/dist-packages (from deep-xf) (3.2.2)\n",
            "Collecting seaborn==0.9.0\n",
            "  Downloading seaborn-0.9.0-py3-none-any.whl (208 kB)\n",
            "\u001b[K     |████████████████████████████████| 208 kB 55.5 MB/s \n",
            "\u001b[?25hRequirement already satisfied: pandas-profiling in /usr/local/lib/python3.7/dist-packages (from deep-xf) (1.4.1)\n",
            "Requirement already satisfied: keras in /usr/local/lib/python3.7/dist-packages (from deep-xf) (2.6.0)\n",
            "Requirement already satisfied: pandas in /usr/local/lib/python3.7/dist-packages (from deep-xf) (1.1.5)\n",
            "Requirement already satisfied: ipython in /usr/local/lib/python3.7/dist-packages (from deep-xf) (5.5.0)\n",
            "Requirement already satisfied: numpy>=1.9.3 in /usr/local/lib/python3.7/dist-packages (from seaborn==0.9.0->deep-xf) (1.19.5)\n",
            "Requirement already satisfied: scipy>=0.14.0 in /usr/local/lib/python3.7/dist-packages (from seaborn==0.9.0->deep-xf) (1.4.1)\n",
            "Collecting slicer==0.0.7\n",
            "  Downloading slicer-0.0.7-py3-none-any.whl (14 kB)\n",
            "Requirement already satisfied: numba in /usr/local/lib/python3.7/dist-packages (from shap==0.39.0->deep-xf) (0.51.2)\n",
            "Requirement already satisfied: cloudpickle in /usr/local/lib/python3.7/dist-packages (from shap==0.39.0->deep-xf) (1.3.0)\n",
            "Requirement already satisfied: patsy>=0.5 in /usr/local/lib/python3.7/dist-packages (from statsmodels==0.12.2->deep-xf) (0.5.2)\n",
            "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->deep-xf) (1.3.2)\n",
            "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib->deep-xf) (0.10.0)\n",
            "Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->deep-xf) (2.8.2)\n",
            "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->deep-xf) (2.4.7)\n",
            "Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from cycler>=0.10->matplotlib->deep-xf) (1.15.0)\n",
            "Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.7/dist-packages (from pandas->deep-xf) (2018.9)\n",
            "Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.7/dist-packages (from scikit-learn>=0.24.0->deep-xf) (1.0.1)\n",
            "Collecting threadpoolctl>=2.0.0\n",
            "  Downloading threadpoolctl-3.0.0-py3-none-any.whl (14 kB)\n",
            "Requirement already satisfied: pickleshare in /usr/local/lib/python3.7/dist-packages (from ipython->deep-xf) (0.7.5)\n",
            "Requirement already satisfied: pexpect in /usr/local/lib/python3.7/dist-packages (from ipython->deep-xf) (4.8.0)\n",
            "Requirement already satisfied: prompt-toolkit<2.0.0,>=1.0.4 in /usr/local/lib/python3.7/dist-packages (from ipython->deep-xf) (1.0.18)\n",
            "Requirement already satisfied: decorator in /usr/local/lib/python3.7/dist-packages (from ipython->deep-xf) (4.4.2)\n",
            "Requirement already satisfied: traitlets>=4.2 in /usr/local/lib/python3.7/dist-packages (from ipython->deep-xf) (5.1.0)\n",
            "Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.7/dist-packages (from ipython->deep-xf) (57.4.0)\n",
            "Requirement already satisfied: simplegeneric>0.8 in /usr/local/lib/python3.7/dist-packages (from ipython->deep-xf) (0.8.1)\n",
            "Requirement already satisfied: pygments in /usr/local/lib/python3.7/dist-packages (from ipython->deep-xf) (2.6.1)\n",
            "Requirement already satisfied: wcwidth in /usr/local/lib/python3.7/dist-packages (from prompt-toolkit<2.0.0,>=1.0.4->ipython->deep-xf) (0.2.5)\n",
            "Requirement already satisfied: jupyter-console in /usr/local/lib/python3.7/dist-packages (from jupyter->deep-xf) (5.2.0)\n",
            "Requirement already satisfied: notebook in /usr/local/lib/python3.7/dist-packages (from jupyter->deep-xf) (5.3.1)\n",
            "Requirement already satisfied: qtconsole in /usr/local/lib/python3.7/dist-packages (from jupyter->deep-xf) (5.1.1)\n",
            "Requirement already satisfied: nbconvert in /usr/local/lib/python3.7/dist-packages (from jupyter->deep-xf) (5.6.1)\n",
            "Requirement already satisfied: ipywidgets in /usr/local/lib/python3.7/dist-packages (from jupyter->deep-xf) (7.6.5)\n",
            "Requirement already satisfied: ipykernel in /usr/local/lib/python3.7/dist-packages (from jupyter->deep-xf) (4.10.1)\n",
            "Requirement already satisfied: tornado>=4.0 in /usr/local/lib/python3.7/dist-packages (from ipykernel->jupyter->deep-xf) (5.1.1)\n",
            "Requirement already satisfied: jupyter-client in /usr/local/lib/python3.7/dist-packages (from ipykernel->jupyter->deep-xf) (5.3.5)\n",
            "Requirement already satisfied: ipython-genutils~=0.2.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets->jupyter->deep-xf) (0.2.0)\n",
            "Requirement already satisfied: nbformat>=4.2.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets->jupyter->deep-xf) (5.1.3)\n",
            "Requirement already satisfied: widgetsnbextension~=3.5.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets->jupyter->deep-xf) (3.5.1)\n",
            "Requirement already satisfied: jupyterlab-widgets>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from ipywidgets->jupyter->deep-xf) (1.0.2)\n",
            "Requirement already satisfied: jsonschema!=2.5.0,>=2.4 in /usr/local/lib/python3.7/dist-packages (from nbformat>=4.2.0->ipywidgets->jupyter->deep-xf) (2.6.0)\n",
            "Requirement already satisfied: jupyter-core in /usr/local/lib/python3.7/dist-packages (from nbformat>=4.2.0->ipywidgets->jupyter->deep-xf) (4.8.1)\n",
            "Requirement already satisfied: Send2Trash in /usr/local/lib/python3.7/dist-packages (from notebook->jupyter->deep-xf) (1.8.0)\n",
            "Requirement already satisfied: terminado>=0.8.1 in /usr/local/lib/python3.7/dist-packages (from notebook->jupyter->deep-xf) (0.12.1)\n",
            "Requirement already satisfied: jinja2 in /usr/local/lib/python3.7/dist-packages (from notebook->jupyter->deep-xf) (2.11.3)\n",
            "Requirement already satisfied: pyzmq>=13 in /usr/local/lib/python3.7/dist-packages (from jupyter-client->ipykernel->jupyter->deep-xf) (22.3.0)\n",
            "Requirement already satisfied: ptyprocess in /usr/local/lib/python3.7/dist-packages (from terminado>=0.8.1->notebook->jupyter->deep-xf) (0.7.0)\n",
            "Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.7/dist-packages (from jinja2->notebook->jupyter->deep-xf) (2.0.1)\n",
            "Requirement already satisfied: bleach in /usr/local/lib/python3.7/dist-packages (from nbconvert->jupyter->deep-xf) (4.1.0)\n",
            "Requirement already satisfied: pandocfilters>=1.4.1 in /usr/local/lib/python3.7/dist-packages (from nbconvert->jupyter->deep-xf) (1.5.0)\n",
            "Requirement already satisfied: defusedxml in /usr/local/lib/python3.7/dist-packages (from nbconvert->jupyter->deep-xf) (0.7.1)\n",
            "Requirement already satisfied: testpath in /usr/local/lib/python3.7/dist-packages (from nbconvert->jupyter->deep-xf) (0.5.0)\n",
            "Requirement already satisfied: mistune<2,>=0.8.1 in /usr/local/lib/python3.7/dist-packages (from nbconvert->jupyter->deep-xf) (0.8.4)\n",
            "Requirement already satisfied: entrypoints>=0.2.2 in /usr/local/lib/python3.7/dist-packages (from nbconvert->jupyter->deep-xf) (0.3)\n",
            "Requirement already satisfied: packaging in /usr/local/lib/python3.7/dist-packages (from bleach->nbconvert->jupyter->deep-xf) (21.0)\n",
            "Requirement already satisfied: webencodings in /usr/local/lib/python3.7/dist-packages (from bleach->nbconvert->jupyter->deep-xf) (0.5.1)\n",
            "Requirement already satisfied: llvmlite<0.35,>=0.34.0.dev0 in /usr/local/lib/python3.7/dist-packages (from numba->shap==0.39.0->deep-xf) (0.34.0)\n",
            "Requirement already satisfied: pywavelets in /usr/local/lib/python3.7/dist-packages (from py-ecg-detectors->deep-xf) (1.1.1)\n",
            "Collecting gatspy\n",
            "  Downloading gatspy-0.3.tar.gz (554 kB)\n",
            "\u001b[K     |████████████████████████████████| 554 kB 63.5 MB/s \n",
            "\u001b[?25hRequirement already satisfied: qtpy in /usr/local/lib/python3.7/dist-packages (from qtconsole->jupyter->deep-xf) (1.11.2)\n",
            "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from torch->deep-xf) (3.7.4.3)\n",
            "Building wheels for collected packages: shap, gatspy\n",
            "  Building wheel for shap (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for shap: filename=shap-0.39.0-cp37-cp37m-linux_x86_64.whl size=491639 sha256=6533e40d67d476f9b7654cda34f8bbdb7123f53f6274d173c883250b734d14d8\n",
            "  Stored in directory: /root/.cache/pip/wheels/ca/25/8f/6ae5df62c32651cd719e972e738a8aaa4a87414c4d2b14c9c0\n",
            "  Building wheel for gatspy (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for gatspy: filename=gatspy-0.3-py3-none-any.whl size=43819 sha256=7a73097402dc73ce2a6f8081d76714e4b31bec0e8af193fb431a0d5819486319\n",
            "  Stored in directory: /root/.cache/pip/wheels/1f/c1/4c/8de278f816259b4f67e2356c0bda260a593375661e400bf31f\n",
            "Successfully built shap gatspy\n",
            "Installing collected packages: threadpoolctl, slicer, scikit-learn, gatspy, statsmodels, shap, seaborn, py-ecg-detectors, deep-xf\n",
            "  Attempting uninstall: scikit-learn\n",
            "    Found existing installation: scikit-learn 0.22.2.post1\n",
            "    Uninstalling scikit-learn-0.22.2.post1:\n",
            "      Successfully uninstalled scikit-learn-0.22.2.post1\n",
            "  Attempting uninstall: statsmodels\n",
            "    Found existing installation: statsmodels 0.10.2\n",
            "    Uninstalling statsmodels-0.10.2:\n",
            "      Successfully uninstalled statsmodels-0.10.2\n",
            "  Attempting uninstall: seaborn\n",
            "    Found existing installation: seaborn 0.11.2\n",
            "    Uninstalling seaborn-0.11.2:\n",
            "      Successfully uninstalled seaborn-0.11.2\n",
            "Successfully installed deep-xf-0.0.5 gatspy-0.3 py-ecg-detectors-1.1.0 scikit-learn-1.0.1 seaborn-0.9.0 shap-0.39.0 slicer-0.0.7 statsmodels-0.12.2 threadpoolctl-3.0.0\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "iZ4mO7DzqBB8"
      },
      "source": [
        "__author__ = 'Ajay'\n",
        "__version__ = '0.0.1'"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9kAv5squqEvN"
      },
      "source": [
        "\"\"\"\n",
        "# Proof-Of-Concept: Siamese Neural Network Model for checking the ECGs signal similarity between the different performed recorded activities\n",
        "\"\"\""
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "sisMAJ_xp0gB",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "187b5890-54d6-4687-9a0c-65e28e186853"
      },
      "source": [
        "from deep_xf.similarity import *"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Imported DeepXF version:0.0.5. Example call by using:\n",
            "\n",
            "******************   SET FORECAST MODEL/BASE CONFIGURATIONS   ********************\n",
            "\n",
            "# select algorithms, scaler, etc.\n",
            "select_model, select_user_path, select_scaler, forecast_window = Forecast.set_model_config(select_model='rnn', select_user_path='./forecast_folder_path/', select_scaler='minmax', forecast_window=1)\n",
            "\n",
            "----------------------------------------------------------------------------------\n",
            "\n",
            "# set variables\n",
            "ts, fc = Forecast.set_variable(ts='Datetime', fc='PJME_MW')\n",
            "\n",
            "# get variables\n",
            "model_df, orig_df = Helper.get_variable(df, ts, fc)\n",
            "\n",
            "----------------------------------------------------------------------------------\n",
            "\n",
            "# set hyperparameters\n",
            "hidden_dim, layer_dim, batch_size, dropout, n_epochs, learning_rate, weight_decay = Forecast.hyperparameter_config(hidden_dim=64,layer_dim = 3, batch_size=64, dropout = 0.2, n_epochs = 30, learning_rate = 1e-3, weight_decay = 1e-6)\n",
            "\n",
            "----------------------------------------------------------------------------------\n",
            "\n",
            "*****************   Calling DEEP LEARNING BASED FORECASTING   *********************\n",
            "\n",
            "# train selected deep learning model\n",
            "opt, scaler = Forecast.train(df=df_full_features, target_col='value', split_ratio=0.2, select_model=select_model, select_scaler=select_scaler, forecast_window=forecast_window, batch_size=batch_size, hidden_dim=hidden_dim, layer_dim=layer_dim,dropout=dropout, n_epochs=n_epochs, learning_rate=learning_rate, weight_decay=weight_decay)\n",
            "\n",
            "----------------------------------------------------------------------------------\n",
            "\n",
            "# forecast for the user provided window\n",
            "forecasted_data, ff_full_features, ff_full_features_ = Forecast.forecast(model_df, ts, fc, opt, scaler, period=25, fq='1h', select_scaler=select_scaler,)\n",
            "\n",
            "---------------------------------------------------------------------------------\n",
            "\n",
            "# model interpretation\n",
            "Helper.explainable_forecast(df_full_features, ff_full_features_, fc, specific_prediction_sample_to_explain=df.shape[0]+2, input_label_index_value=0, num_labels=1)\n",
            "\n",
            "\n",
            "******************   DYNAMIC FACTOR MODEL BASED NOWCASTING   *********************\n",
            "\n",
            "# select algorithms, scaler, etc.\n",
            "select_model, select_user_path, select_scaler, forecast_window = Forecast.set_model_config(select_model='em', select_user_path='./forecast_folder_path/', select_scaler='minmax', forecast_window=5)\n",
            "\n",
            "----------------------------------------------------------------------------------\n",
            "\n",
            "# train nowcasting model with EM-algorithm\n",
            "nowcast_full_data, nowcast_pred_data = EMModel.nowcast(df_full_features, ts, fc, period=5, fq='1h', forecast_window=forecast_window, select_model=select_model)\n",
            "\n",
            "----------------------------------------------------------------------------------\n",
            "\n",
            "# get interpretable results\n",
            "EMModel.explainable_nowcast(df_full_features, nowcast_pred_data, fc, specific_prediction_sample_to_explain=df.shape[0]+2, input_label_index_value=0, num_labels=1)\n",
            "\n",
            "***********************   DENOISING TIMESERIES ECG SIGNALS   **********************\n",
            "\n",
            "# set thresholds\n",
            "fs, order, cutoff_high, cutoff_low, powerline, nyq = Denoise.set_parameters(fs = 500, order = 5, cutoff_high = 0.5, cutoff_low = 2, powerline = 60)\n",
            "\n",
            "# filter noise and save output\n",
            "Denoise.plot_and_write(ecg_signal_data, featurecol_index=2, fs=fs, cutoff_high=cutoff_high, cutoff_low=cutoff_low, nyq = nyq, powerline=powerline, order=order)\n",
            "\n",
            "\n",
            "***********************   TIMESERIES ECG SIGNAL SIMILARITY   **********************\n",
            "\n",
            "# configure, and train siamese neural network\n",
            "sm = Siamese.Siamese_Model(5) # 5 - # number of features\n",
            "sm.compile(loss='mse', optimizer='adam', metrics=['mse', 'mae', 'mape', 'cosine'])\n",
            "score = sm(inputs=[np.array(activity_input_sequence_1), np.array(activity_input_sequence_2)]) # signal similarity scores\n",
            "\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Nu_JX11IqdHY"
      },
      "source": [
        "# import datasets\n",
        "\n",
        "walking_normal_pace_data = pd.read_csv('https://raw.githubusercontent.com/ajayarunachalam/Deep_XF/master/processed_data_ecg/processed_walking_normal_pace.csv')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "DvDkgCRCrvnE",
        "outputId": "c00e5db7-ac2d-41e2-9b82-cc18254ad647"
      },
      "source": [
        "walking_normal_pace_data.shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(30000, 7)"
            ]
          },
          "metadata": {},
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 111
        },
        "id": "Y7zETvu8qzH5",
        "outputId": "9791b669-f269-4465-8480-a9ebff32a789"
      },
      "source": [
        "walking_normal_pace_data.head(2)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Date</th>\n",
              "      <th>Time</th>\n",
              "      <th>ecg1</th>\n",
              "      <th>ecg2</th>\n",
              "      <th>ecg3</th>\n",
              "      <th>ecg4</th>\n",
              "      <th>ecg5</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>2021-04-07</td>\n",
              "      <td>16:21:15.513000</td>\n",
              "      <td>0.000005</td>\n",
              "      <td>0.000048</td>\n",
              "      <td>-0.000041</td>\n",
              "      <td>0.000359</td>\n",
              "      <td>-0.000107</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>2021-04-07</td>\n",
              "      <td>16:21:15.515000</td>\n",
              "      <td>0.000122</td>\n",
              "      <td>0.000258</td>\n",
              "      <td>-0.000091</td>\n",
              "      <td>0.000411</td>\n",
              "      <td>-0.000029</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "         Date             Time      ecg1  ...      ecg3      ecg4      ecg5\n",
              "0  2021-04-07  16:21:15.513000  0.000005  ... -0.000041  0.000359 -0.000107\n",
              "1  2021-04-07  16:21:15.515000  0.000122  ... -0.000091  0.000411 -0.000029\n",
              "\n",
              "[2 rows x 7 columns]"
            ]
          },
          "metadata": {},
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1VJqvE39rR0U"
      },
      "source": [
        "walking_fast_pace_data = pd.read_csv('https://raw.githubusercontent.com/ajayarunachalam/Deep_XF/master/processed_data_ecg/processed_walking_fast_pace.csv')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "UTOQrCxeryQG",
        "outputId": "399cc38f-90c1-4e24-8413-544012fc10a4"
      },
      "source": [
        "walking_fast_pace_data.shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(20000, 7)"
            ]
          },
          "metadata": {},
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 111
        },
        "id": "UuWJDbyXrpfO",
        "outputId": "94d0c9a6-89ec-43a4-a51c-2b0142a018bb"
      },
      "source": [
        "walking_fast_pace_data.head(2)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Date</th>\n",
              "      <th>Time</th>\n",
              "      <th>ecg1</th>\n",
              "      <th>ecg2</th>\n",
              "      <th>ecg3</th>\n",
              "      <th>ecg4</th>\n",
              "      <th>ecg5</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>2021-04-07</td>\n",
              "      <td>16:23:05.185000</td>\n",
              "      <td>-0.036633</td>\n",
              "      <td>-0.000327</td>\n",
              "      <td>0.011026</td>\n",
              "      <td>0.012079</td>\n",
              "      <td>0.011707</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>2021-04-07</td>\n",
              "      <td>16:23:05.187000</td>\n",
              "      <td>-0.050022</td>\n",
              "      <td>-0.000012</td>\n",
              "      <td>0.012225</td>\n",
              "      <td>0.015107</td>\n",
              "      <td>0.016303</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "         Date             Time      ecg1  ...      ecg3      ecg4      ecg5\n",
              "0  2021-04-07  16:23:05.185000 -0.036633  ...  0.011026  0.012079  0.011707\n",
              "1  2021-04-07  16:23:05.187000 -0.050022  ...  0.012225  0.015107  0.016303\n",
              "\n",
              "[2 rows x 7 columns]"
            ]
          },
          "metadata": {},
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "IttlbndSrrpr"
      },
      "source": [
        "activity_input_sequence_1 = walking_normal_pace_data.iloc[0:20000:,2:]"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Q1Ce9_X7r2jg"
      },
      "source": [
        "activity_input_sequence_2 = walking_fast_pace_data.iloc[:,2:]"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "U2OOuZNNr5M9",
        "outputId": "5211139b-4d58-4fc9-8a14-d741218c6bf5"
      },
      "source": [
        "activity_input_sequence_1.shape, activity_input_sequence_2.shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "((20000, 5), (20000, 5))"
            ]
          },
          "metadata": {},
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zQBYHuqDr62r"
      },
      "source": [
        "sm = Siamese.Siamese_Model(5)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "90IWDOvVsHb7",
        "outputId": "209ec363-ca96-4b44-caa2-8048fd7f2bec"
      },
      "source": [
        "sm.summary()"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Model: \"model\"\n",
            "__________________________________________________________________________________________________\n",
            "Layer (type)                    Output Shape         Param #     Connected to                     \n",
            "==================================================================================================\n",
            "input_1 (InputLayer)            [(None, 5)]          0                                            \n",
            "__________________________________________________________________________________________________\n",
            "input_2 (InputLayer)            [(None, 5)]          0                                            \n",
            "__________________________________________________________________________________________________\n",
            "subtract (Subtract)             (None, 5)            0           input_1[0][0]                    \n",
            "                                                                 input_2[0][0]                    \n",
            "__________________________________________________________________________________________________\n",
            "multiply (Multiply)             (None, 5)            0           subtract[0][0]                   \n",
            "                                                                 subtract[0][0]                   \n",
            "__________________________________________________________________________________________________\n",
            "multiply_1 (Multiply)           (None, 5)            0           input_1[0][0]                    \n",
            "                                                                 input_2[0][0]                    \n",
            "__________________________________________________________________________________________________\n",
            "concatenate (Concatenate)       (None, 10)           0           multiply[0][0]                   \n",
            "                                                                 multiply_1[0][0]                 \n",
            "__________________________________________________________________________________________________\n",
            "dense (Dense)                   (None, 64)           384         multiply[0][0]                   \n",
            "__________________________________________________________________________________________________\n",
            "dense_1 (Dense)                 (None, 64)           384         multiply_1[0][0]                 \n",
            "__________________________________________________________________________________________________\n",
            "dense_2 (Dense)                 (None, 64)           704         concatenate[0][0]                \n",
            "__________________________________________________________________________________________________\n",
            "batch_normalization (BatchNorma (None, 64)           256         dense[0][0]                      \n",
            "__________________________________________________________________________________________________\n",
            "batch_normalization_1 (BatchNor (None, 64)           256         dense_1[0][0]                    \n",
            "__________________________________________________________________________________________________\n",
            "batch_normalization_2 (BatchNor (None, 64)           256         dense_2[0][0]                    \n",
            "__________________________________________________________________________________________________\n",
            "dropout (Dropout)               (None, 64)           0           batch_normalization[0][0]        \n",
            "__________________________________________________________________________________________________\n",
            "dropout_1 (Dropout)             (None, 64)           0           batch_normalization_1[0][0]      \n",
            "__________________________________________________________________________________________________\n",
            "dropout_2 (Dropout)             (None, 64)           0           batch_normalization_2[0][0]      \n",
            "__________________________________________________________________________________________________\n",
            "activation (Activation)         (None, 64)           0           dropout[0][0]                    \n",
            "__________________________________________________________________________________________________\n",
            "activation_1 (Activation)       (None, 64)           0           dropout_1[0][0]                  \n",
            "__________________________________________________________________________________________________\n",
            "activation_2 (Activation)       (None, 64)           0           dropout_2[0][0]                  \n",
            "__________________________________________________________________________________________________\n",
            "concatenate_1 (Concatenate)     (None, 192)          0           activation[0][0]                 \n",
            "                                                                 activation_1[0][0]               \n",
            "                                                                 activation_2[0][0]               \n",
            "__________________________________________________________________________________________________\n",
            "dense_3 (Dense)                 (None, 256)          49408       concatenate_1[0][0]              \n",
            "__________________________________________________________________________________________________\n",
            "batch_normalization_3 (BatchNor (None, 256)          1024        dense_3[0][0]                    \n",
            "__________________________________________________________________________________________________\n",
            "dropout_3 (Dropout)             (None, 256)          0           batch_normalization_3[0][0]      \n",
            "__________________________________________________________________________________________________\n",
            "activation_3 (Activation)       (None, 256)          0           dropout_3[0][0]                  \n",
            "__________________________________________________________________________________________________\n",
            "dense_4 (Dense)                 (None, 1)            257         activation_3[0][0]               \n",
            "==================================================================================================\n",
            "Total params: 52,929\n",
            "Trainable params: 52,033\n",
            "Non-trainable params: 896\n",
            "__________________________________________________________________________________________________\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mRo4OlY6sKdv"
      },
      "source": [
        "sm.compile(loss='mse', optimizer='adam', metrics=['mse', 'mae', 'mape', 'cosine'])"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QLjORtiUsOXe"
      },
      "source": [
        "score = sm(inputs=[np.array(activity_input_sequence_1), np.array(activity_input_sequence_2)])"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "iqepqTc_sQhJ",
        "outputId": "b31317e2-20de-47f1-812d-71e036dc261b"
      },
      "source": [
        "score.shape"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "TensorShape([20000, 1])"
            ]
          },
          "metadata": {},
          "execution_count": 16
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "f3igKW6vsVj6",
        "outputId": "840ee144-9b6e-4ea5-f959-766738fa042d"
      },
      "source": [
        "score"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(20000, 1), dtype=float32, numpy=\n",
              "array([[6.8679612e-05],\n",
              "       [1.3716531e-04],\n",
              "       [7.6512610e-05],\n",
              "       ...,\n",
              "       [1.0540376e-07],\n",
              "       [7.8860651e-08],\n",
              "       [2.2962999e-08]], dtype=float32)>"
            ]
          },
          "metadata": {},
          "execution_count": 17
        }
      ]
    }
  ]
}